import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:oktoast/oktoast.dart';
import 'package:wan_android_flutter/common/resources/colors/dark_color_data.dart';
import 'package:wan_android_flutter/common/resources/colors/light_color_data.dart';
import 'package:wan_android_flutter/common/resources/text_styles.dart';
import 'package:wan_android_flutter/modules/splash/splash_binding.dart';
import 'package:wan_android_flutter/generated/locales.g.dart' as translation;

import '../../generated/locales.g.dart';
import '../resources/colors/color_data.dart';
import '../routes/page_manager.dart';
import '../routes/route_manager.dart';

class Application extends StatefulWidget {
  static final GlobalKey<NavigatorState> applicationKey = GlobalKey<NavigatorState>();

  const Application({super.key});

  @override
  State<Application> createState() => _ApplicationState();
}

class _ApplicationState extends State<Application> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  Widget build(BuildContext context) {

    final botToastBuilder = BotToastInit();
    return GetMaterialApp(
      onGenerateTitle: (BuildContext context) {
        return LocaleKeys.app_name.tr;
      },
      theme: ThemeData.light().copyWith(extensions: [lightColorData]), /// 浅色主题
      darkTheme: ThemeData.dark().copyWith(extensions: [darkColorData]), /// 深色主题
      translationsKeys: translation.AppTranslation.translations, /// get_cli脚本生成的语言文本
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('zh', 'CN'), /// 简体中文
        Locale('en', 'US'), /// 英文
      ], /// 支持的语言列表
      locale: Get.deviceLocale, /// 跟随手机系统语言
      fallbackLocale: const Locale('zh', 'CN'), /// 找不到指定语言时默认的语言
      navigatorKey: Application.applicationKey,
      debugShowCheckedModeBanner: false,
      getPages: PageManager.pages,
      initialRoute: RouteManager.splashPage,
      initialBinding: SplashBinding(),
      navigatorObservers: [
        BotToastNavigatorObserver(),
      ],
      builder: (BuildContext context, Widget? child) {
        return botToastBuilder(
            context,
            Material(
              elevation: 0,
              color: Colors.transparent,
              child: OKToast(
                backgroundColor: ColorData.of(context).bgWhite,
                textPadding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
                radius: 8.0,
                movingOnWindowChange: false,
                position: ToastPosition.top,
                textStyle: ColorData.of(context).textBlack.s16.regular,
                child: child!,
                animationBuilder: (
                  BuildContext context,
                  Widget child,
                  AnimationController controller,
                  double percent,
                ) {
                  return Opacity(opacity: percent, child: child);
                },
              ),
            ));
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.inactive:
        // 应用程序处于闲置状态并且没有收到用户的输入事件。
        // 注意这个状态，在切换到后台时候会触发，所以流程应该是先冻结窗口，然后停止UI
        if (kDebugMode) {
          print('YM----->AppLifecycleState.inactive');
        }
        break;
      case AppLifecycleState.paused:
        // 应用程序处于不可见状态
        if (kDebugMode) {
          print('YM----->AppLifecycleState.paused');
        }
        showToast("应用退到后台啦");
        break;
      case AppLifecycleState.resumed:
        // 进入应用时候不会触发该状态
        // 应用程序处于可见状态，并且可以响应用户的输入事件。它相当于 Android 中Activity的onResume。
        if (kDebugMode) {
          print('YM----->AppLifecycleState.resumed');
        }
        showToast("应用回到前台啦");
        break;
      case AppLifecycleState.detached:
        // 当前页面即将退出
        if (kDebugMode) {
          print('YM----->AppLifecycleState.detached');
        }
        break;
      case AppLifecycleState.hidden:
        if (kDebugMode) {
          print('YM----->AppLifecycleState.detached');
        }
        break;
    }
  }
}
